import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'LoadToast.dart';
import 'ShowToast.dart';

///定义一个通道,flutter调用原生方法
const platform = const MethodChannel('com.RfidRead.rfidread');

class SetPower extends StatefulWidget {

    SetPower({Key? key,required this.SetPowerType}) : super(key: key);
var SetPowerType;
  @override
  State<SetPower> createState() => _SetPowerState();
}

class _SetPowerState extends State<SetPower> {
  TextEditingController PowerController = new TextEditingController();
  int Power=0;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Future.delayed(Duration.zero, () async {
      //查询本地保存的功率，如果没有就查询硬件功率
      SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
      print("本地保存的key${widget.SetPowerType}");
      print("前一个页面是:${widget.SetPowerType}");
      print("本地保存的功率${await sharedPreferences.getInt(widget.SetPowerType)}");
      if(await sharedPreferences.getInt(widget.SetPowerType)==null){
      //  本地没有保存，读取硬件功率
        //  获取功率
        Power = await platform.invokeMethod('GetPower');
      }
      else{
        Power = (await sharedPreferences.getInt(widget.SetPowerType))!;
      }
      // 给输入框赋值
      PowerController = new TextEditingController(text: Power.toString());
      setState(() {

      });

    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("设置功率"),
        centerTitle: true,
      ),
      body: Container(
        padding:EdgeInsets.only(left: 30,right: 30,top: 30),
        child: Center(
          child: Column(
            children: [
              Container(
                child: Center(
                  child: Text("请设置功率",style: TextStyle(fontSize: 25,fontWeight: FontWeight.w600),),
                ),
              ),
              
              TextField(
                controller: PowerController,
                decoration: InputDecoration(
                  prefixText: "当前功率:  ",
                    suffix: MaterialButton(
                      child: Text(
                        "设置",
                        style: TextStyle(color: Colors.white),
                      ),
                      color: Colors.blue,
                      onPressed: () async {
                        SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
                        //  设置功率
                        bool IsSet = await platform.invokeMethod(
                            'SetPower', int.parse(PowerController.text));
                        if (IsSet) {
                          await sharedPreferences.setInt(widget.SetPowerType, int.parse(PowerController.text));
                          ShowToast().MyToast("设置成功!");
                          //  返回页面
                          Navigator.pop(context);
                        }
                        else{
                          ShowToast().MyToast("设置失败!");
                        }
                      },
                    )
                ),
              ),
            ],
          )
        ),
      ),
    );
  }

}
